home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / SpellCompositor / InteractionFrame.java < prev    next >
Encoding:
Java Source  |  2001-06-23  |  12.8 KB  |  433 lines

  1. /*
  2.     Interaction Frame by Scott Ziegler 08.19.99
  3.     
  4.     This file and its intellectual contents are considered property of the creator and are to be treated as such with respect to use in other applications.  Any use of this software for any purpose whatsoever must have explicit permission from the author of the file.  This code is part of an ongoing development process for future possible products, and so is considered private property.
  5. Do not use without permission.  Author: Scott C. Ziegler <Aslan@Narnia.net>
  6.     
  7.     Instructions for use:
  8.     
  9.                     This program is used as an interface by which to textually control programs on a 
  10.             prompt-response basis. The following functions provide the behaviors to integrate its
  11.             use with user-defined classes, allowing for testing and use.
  12.             
  13.             The functions:
  14.                     
  15.                     String     getLine() - waits for user input to be entered at the prompt, and returns the
  16.                                                             entered string.
  17.                                                             
  18.                     void     printLine(String s) - prints the string s and provides a newline ("\n")
  19.                     
  20.                     void    printEndLine(String s) - prints the string s and gives a newline and
  21.                                                                                             a user prompt. (">")
  22.                                                                                             
  23.                     void    print(String s) - appends the string s with no newline.
  24.                     
  25.                     void    endLine() - appends a newline and a user prompt. (to be used in combination
  26.                                                             with print(String s).)
  27.                                                             
  28.     
  29.     Code in use by:
  30.             FileTester
  31.             DiceTester
  32.             SpellCompositor
  33.     
  34.     vers. history:
  35.     
  36.     05.16.00 -    Corrected the default button nonsense to just an action of the JTextField
  37.     07.20.00 -  Declared the project Syntactically Correct as stands.
  38.     07.20.00 -  added functionality for interaction with other classes. 
  39.                             i.e. getLine()/printLine(), etc.
  40.     07.20.00 -  removed all old debugging code.
  41.     07.24.00 -  added feature that scrolls viewpane as new text appears.
  42.     07.24.00 -    integrated BasicWindowMonitor into the class file.
  43.     09.27.00 -     changed implementation of print statements to differentiate between
  44.                             printing a system prompt or a user prompt, making the system prompt
  45.                             a optional string or no string.  Added print(), printEndLine(), endLine(),
  46.                             and newLine().
  47.     06.12.01 -    began work on the OSX version of this app.
  48.     
  49.     
  50.     Work needed:
  51.     
  52.             - need a method of scripting commands from a file or a series of text inputs or text input format
  53.             - need accessors to add/remove menuItems
  54.             - Panel of buttons and lists to clear and to access history
  55.             - fix the text field to be as large as the width minus the button always.
  56.     
  57.     Work Completed:
  58.             
  59.             found function (JButton.doClick(int millisecs)) to activate button to prevent redundancy
  60.             examine focus to be always returned to the text field.     -Finished ??.??.??
  61.             discover why the default button is not working...         -Finished 05.16.00
  62.             
  63. */
  64.  
  65. import java.awt.*;
  66. import java.awt.event.*;
  67. import java.awt.Window;
  68. import java.util.*;
  69. import java.io.*;
  70. import javax.swing.*;
  71. import javax.swing.border.*;
  72. import javax.swing.event.*;
  73.  
  74.  
  75. public class InteractionFrame extends JFrame {
  76.  
  77.     public static final String DEFAULT_SYS_PROMPT = "Sys: ";
  78.  
  79.     JScrollPane         scrollingRegion;
  80.     JTextArea            textDisplayRegion;
  81.     JTextField            textEntryRegion;
  82.     JButton                executeButton;
  83.     JPanel                commandPanel;
  84.     JRootPane            root;
  85.     JFileChooser        scriptChooser;
  86.     JFrame                frame;
  87.     
  88.     JMenuBar            menuBar;
  89.     JMenu                fileMenu;
  90.         JMenuItem            aboutBoxMenuItem;
  91.         JMenuItem            quitMenuItem;
  92.         JMenuItem            runScriptMenuItem;
  93.     JMenu                editMenu;
  94.         JMenuItem            undoMenuItem;
  95.         JMenuItem            cutMenuItem;
  96.         JMenuItem            copyMenuItem;
  97.         JMenuItem            pasteMenuItem;
  98.     JMenu                optionsMenu;
  99.         JCheckBoxMenuItem     promptCBMenuItem;
  100.         
  101.     File        scriptFile;
  102.     boolean        scriptMode;
  103.     
  104.     Vector        scriptVector;
  105.     Enumeration    scriptEnumeration;
  106.     
  107.     Font        systemFont;
  108.     Font        inputFont; 
  109.     
  110.     String        commandString;
  111.     boolean        validString;
  112.     
  113.     String        sysPrompt;
  114.     boolean        sysPromptSwitch;
  115.     
  116.     JScrollBar             jsb;
  117.     BoundedRangeModel     scrollModel;
  118.     int                    oldMax;
  119.     
  120.     public InteractionFrame() {
  121.         
  122.         super ("Spell Compositor - Arcana SpellBook");
  123.         setSize(500, 500);
  124.         addWindowListener(new BasicWindowMonitor());
  125.         frame = this;
  126.         
  127.         Insets objectInsets = new Insets(2,2,2,2);
  128.         
  129.         setValidString(false);
  130.         setSysPromptSwitch(false);
  131.         setSysPrompt(DEFAULT_SYS_PROMPT);
  132.         
  133.         scriptChooser = new JFileChooser();
  134.         scriptVector = new Vector();
  135.         
  136.         menuBar = new JMenuBar();
  137.         menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
  138.         fileMenu = new JMenu("File");
  139.         fileMenu.setMnemonic('F');
  140.         aboutBoxMenuItem = new JMenuItem("About…");
  141.         fileMenu.add(aboutBoxMenuItem);
  142.         aboutBoxMenuItem.addActionListener(new ActionListener() {
  143.             public void actionPerformed(ActionEvent ae) {
  144.                 printEndLine("\nThoth Interaction System \n" +
  145.                              "Written by Scott Ziegler \n" +
  146.                              "¢ Simulcra 1999");
  147.                 }
  148.             });
  149.         runScriptMenuItem = new JMenuItem("Run Script…");
  150.         runScriptMenuItem.addActionListener(new ActionListener() {
  151.             public void actionPerformed(ActionEvent ae) {
  152.                 int option = scriptChooser.showOpenDialog(frame);
  153.                 if (option == JFileChooser.APPROVE_OPTION) {
  154.                     scriptMode = true;
  155.                     scriptFile = scriptChooser.getSelectedFile();
  156.                     runScript();
  157.                     }
  158.                 }
  159.             });
  160.         fileMenu.add(runScriptMenuItem);
  161.         quitMenuItem = new JMenuItem("Quit");
  162.         fileMenu.add(quitMenuItem);
  163.         quitMenuItem.addActionListener(new ActionListener() {
  164.             public void actionPerformed(ActionEvent ae) {
  165.                 System.exit(0);
  166.                 }
  167.             });
  168.         editMenu = new JMenu("Edit");
  169.         undoMenuItem = new JMenuItem("Undo");
  170.         editMenu.add(undoMenuItem);
  171.         
  172.         cutMenuItem = new JMenuItem("Cut");
  173.         editMenu.add(cutMenuItem);
  174.         cutMenuItem.addActionListener(new ActionListener() {
  175.             public void actionPerformed(ActionEvent ae) {
  176.                 //System.exit(0);
  177.                 }
  178.             });
  179.         copyMenuItem = new JMenuItem("Copy");
  180.         editMenu.add(copyMenuItem);
  181.         copyMenuItem.addActionListener(new ActionListener() {
  182.             public void actionPerformed(ActionEvent ae) {
  183.                 //System.exit(0);
  184.                 }
  185.             });
  186.         pasteMenuItem = new JMenuItem("Paste");
  187.         editMenu.add(pasteMenuItem);
  188.         pasteMenuItem.addActionListener(new ActionListener() {
  189.             public void actionPerformed(ActionEvent ae) {
  190.                 //System.exit(0);
  191.                 }
  192.             });
  193.         optionsMenu = new JMenu("Options");
  194.         promptCBMenuItem = new JCheckBoxMenuItem("Use System Prompt", false);
  195.         optionsMenu.add(promptCBMenuItem);
  196.         promptCBMenuItem.addActionListener(new ActionListener() {
  197.             public void actionPerformed(ActionEvent ae) {
  198.                 setSysPromptSwitch(promptCBMenuItem.getState());
  199.                 }
  200.             });
  201.         menuBar.add(fileMenu);
  202.         menuBar.add(editMenu);
  203.         menuBar.add(optionsMenu);
  204.         
  205.         systemFont = new Font("Courier", Font.PLAIN, 10);
  206.         inputFont = new Font("Monaco", Font.PLAIN, 9);
  207.         
  208.         textDisplayRegion = new JTextArea();
  209.         textDisplayRegion.setEditable(false);
  210.         textDisplayRegion.setLineWrap(true);
  211.         textDisplayRegion.setWrapStyleWord(true);
  212.         textDisplayRegion.setFont(systemFont);
  213.         // textDisplayRegion.setInsets(objectInsets);
  214.         scrollingRegion = new JScrollPane(textDisplayRegion);
  215.         scrollingRegion.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  216.         // LineBorder line = new LineBorder(Color.black);     // WORKS
  217.         // scrollingRegion.setViewportBorder(line);         // WORKS
  218.         
  219.         jsb = scrollingRegion.getVerticalScrollBar();
  220.         scrollModel = jsb.getModel();
  221.         oldMax = 0;
  222.         scrollModel.addChangeListener(new ChangeListener() {
  223.             public void stateChanged(ChangeEvent ce) {
  224.                 BoundedRangeModel sourceModel = (BoundedRangeModel)ce.getSource();
  225.                 int newMax = sourceModel.getMaximum();
  226.                 if (newMax > oldMax) {
  227.                     scrollModel.setValue(newMax);
  228.                     oldMax = newMax;
  229.                     }
  230.                 }
  231.             });
  232.         
  233.         
  234.         commandPanel = new JPanel();
  235.         commandPanel.setLayout(new GridBagLayout());
  236.         GridBagConstraints c = new GridBagConstraints();
  237.         
  238.         executeButton = new JButton("Execute");
  239.         executeButton.addActionListener(new ActionListener() {    
  240.             public void actionPerformed(ActionEvent ae) {
  241.                 setCommandString(getCommandText());
  242.                 displayText();
  243.                 setValidString(true);
  244.                 }
  245.             });
  246.         
  247.         textEntryRegion = new JTextField();
  248.         textEntryRegion.setFont(inputFont);
  249.         textEntryRegion.setColumns(66);
  250.         // Code to register the ENTER key to display.
  251.         textEntryRegion.addActionListener(new ActionListener() {
  252.             public void actionPerformed(ActionEvent ev) {
  253.                 executeButton.doClick(55); // click button for 55 milliseconds
  254.                 }
  255.             });    
  256.     
  257.         c.gridx = 0; c.gridy = 0; c.gridheight = 1; c.gridwidth = 1;
  258.         c.weightx = c.weighty = 1.0;
  259.         c.fill = GridBagConstraints.HORIZONTAL;
  260.         c.anchor = GridBagConstraints.CENTER;
  261.         c.insets = objectInsets;
  262.         commandPanel.add(textEntryRegion, c);
  263.         
  264.         c.gridx = 1; c.gridy = 0; c.gridheight = 1; c.gridwidth = 1;
  265.         c.weightx = c.weighty = 0.0;
  266.         c.fill = GridBagConstraints.NONE;
  267.         c.anchor = GridBagConstraints.CENTER;
  268.         c.insets = objectInsets;
  269.         commandPanel.add(executeButton, c);
  270.         
  271.         // initialize window, add components, and show
  272.         
  273.         setJMenuBar(menuBar);
  274.         Container progFrameContentPane = getContentPane();
  275.         progFrameContentPane.add(commandPanel, BorderLayout.SOUTH);
  276.         progFrameContentPane.add(scrollingRegion, BorderLayout.CENTER);
  277.         
  278.         setVisible(true);
  279.         textEntryRegion.requestFocus();
  280.         initSession();
  281.         }
  282.                 
  283.     public String getCommandString() {
  284.         return commandString;
  285.         }
  286.         
  287.     public void setCommandString(String s) {
  288.         commandString = s;
  289.         }
  290.         
  291.     public String getSysPrompt() {
  292.         return sysPrompt;
  293.         }
  294.         
  295.     public void setValidString(boolean b) {
  296.         validString = b;
  297.         }    
  298.     
  299.     public void setSysPromptSwitch(boolean b) {
  300.         sysPromptSwitch = b;
  301.         }
  302.         
  303.     public void setSysPrompt(String s) {
  304.         sysPrompt = s;
  305.         }
  306.         
  307.     public boolean isValidString() {    
  308.         return validString;
  309.         }
  310.     
  311.     public boolean isSysPromptSwitch() {
  312.         return sysPromptSwitch;
  313.         }
  314.                 
  315.     public String getLine() {
  316.         if (scriptMode) {
  317.             return getScriptLine();
  318.             }
  319.         
  320.         String gottenString;
  321.         
  322.         while (!isValidString()) {} // Wait for input to be ready (button press)
  323.         
  324.         gottenString = getCommandString();
  325.         setValidString(false);
  326.         return gottenString;
  327.         }
  328.     
  329.     private String getScriptLine() {
  330.         String statement = new String("");
  331.         if (scriptEnumeration.hasMoreElements()) {
  332.             statement = (String)scriptEnumeration.nextElement();
  333.             return statement;
  334.             }
  335.         else {
  336.             scriptMode = false;
  337.             scriptVector.removeAllElements();
  338.             return getLine(); // if no more statements, call old getLine() again.
  339.             }
  340.         }
  341.     
  342.     public void printLine(String s) {
  343.         if (isSysPromptSwitch()) {
  344.             textDisplayRegion.append(getSysPrompt());
  345.             }
  346.         textDisplayRegion.append(s);
  347.         // clearText(); // ??? not needed I think
  348.         newLine();
  349.         textEntryRegion.requestFocus();
  350.         }
  351.         
  352.     public void printEndLine(String s) {
  353.         if (isSysPromptSwitch()) {
  354.             textDisplayRegion.append(getSysPrompt());
  355.             }
  356.         textDisplayRegion.append(s);
  357.         newLine();
  358.         newPrompt();
  359.         textEntryRegion.requestFocus();
  360.         }
  361.         
  362.     public void print(String s) {
  363.         textDisplayRegion.append(s);
  364.         }
  365.         
  366.     // for combination with print calls
  367.     public void endLine() {    
  368.         newLine();
  369.         newPrompt();
  370.         textEntryRegion.requestFocus();
  371.         }
  372.         
  373.     private String getCommandText() {
  374.         String s = textEntryRegion.getText();
  375.         if (s.length() != 0) return s;
  376.         else return "";
  377.         }
  378.         
  379.     private void initSession() {    
  380.         textDisplayRegion.append("Thoth Interaction System \n");
  381.         textDisplayRegion.append("Written by Scott Ziegler \n");
  382.         textDisplayRegion.append("¢ Simulcra 1999 \n");
  383.         textDisplayRegion.append("System Ready.");
  384.         newLine();
  385.         newPrompt();
  386.         textEntryRegion.requestFocus();
  387.         }
  388.         
  389.     private void displayText() {
  390.         String fieldText = getCommandText();
  391.         if (fieldText.length() != 0) {
  392.             textDisplayRegion.append(fieldText);
  393.             clearText();
  394.             newLine();
  395.             }
  396.         textEntryRegion.requestFocus();
  397.         }
  398.         
  399.     private void clearText() {    
  400.         textEntryRegion.setText("");
  401.         }
  402.         
  403.     private void newPrompt() {
  404.         textDisplayRegion.append(">");
  405.         }
  406.     
  407.     public void newLine() {
  408.         textDisplayRegion.append("\n");
  409.         }
  410.     
  411.     private void runScript() {
  412.         if (scriptMode) {
  413.             try {
  414.                 FileReader fr = new FileReader(scriptFile);
  415.                 BufferedReader in = new BufferedReader(fr);
  416.                 String statement = new String("");
  417.                 
  418.                 statement = in.readLine();
  419.                 while(!(statement.compareTo("<End>") == 0)) {
  420.                     scriptVector.addElement(statement);
  421.                     statement = in.readLine();
  422.                     }
  423.                 in.close();
  424.                 scriptEnumeration = scriptVector.elements();
  425.                 }
  426.             catch(IOException ioe) {
  427.                 System.out.println("Error: IO Error! " + ioe.toString());
  428.                 }
  429.             }
  430.         }
  431.         
  432.     }
  433.